package Zoo.handler;


import Zoo.entity.ZooparkEntity;
import Zoo.impl.LeaderServiceImpl;
import Zoo.service.LeaderService;
import Zoo.view.leaderview.LeaderView;
import Zoo.view.leaderview.UpdateZooparkView;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 园长修改园区界面处理类
 */

public class UpdateZooparkViewHandler implements ActionListener {
    private UpdateZooparkView updateZooparkView;
    private LeaderView leaderView;


    public UpdateZooparkViewHandler(UpdateZooparkView updateZooparkView, LeaderView leaderView) {
        this.updateZooparkView = updateZooparkView;
        this.leaderView = leaderView;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jButton = (JButton) e.getSource();
        String text = jButton.getText();
        if ("修改".equals(text)) {
            LeaderService leaderService = new LeaderServiceImpl();
            ZooparkEntity zooparkEntity = updateZooparkView.buildUpdatedZooparkEntity();
            boolean updateResult = leaderService.update(zooparkEntity);
            if (updateResult) {
                //重新加载表格查到最新数据
                leaderView.reloadTable();
                updateZooparkView.dispose();
            } else {
                JOptionPane.showMessageDialog(updateZooparkView, "修改失败");

            }

        }
    }

}
